JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

870,440 Responses

  1. The mind brain injury law vancouver legal representatives at DE CARO & KAPLEN, LLP.

  2. Guide To Online Shopping Sites In United Kingdom: The Intermediate Guide
    Towards Online Shopping Sites In United Kingdom online shopping sites in united kingdom

  3. Richardorele表示:

    where can you buy prednisone: prednisone buy canada – where can i get prednisone over the counter

  4. The Comprehensive Guide To Erb’s Palsy Claim galveston erb’s palsy Law firm

  5. The Secret Secrets Of Untreated Adhd In Adults Treat Adult Adhd

  6. I am genuinely grateful to the owner of this web page who has shared this wonderful post
    at here.

  7. Tania表示:

    You’ll Never Be Able To Figure Out This 18 Wheeler Accident
    Law Firm’s Benefits 18 wheeler accident law firm (Tania)

  8. 20 Questions You Should Have To Ask About Coffee Machines With Beans Before You Decide To Purchase It Coffee machine beans to Cup

  9. Henryfet表示:

    how much is generic neurontin: neurontin price uk – neurontin 30 mg

  10. Why You Should Not Think About Making Improvements To Your Double Glazed Units Manufacturers Near Me replace double glazed units

  11. Charleselila表示:

    amoxicillin pills 500 mg: amoxicillin 500mg over the counter – buy amoxicillin 250mg

  12. Ask Me Anything: 10 Responses To Your Questions About Upvc Windows Repair repair
    upvc window (articlement.Com)

  13. Charleselila表示:

    how can i order prednisone: buy prednisone from canada – prednisone 20mg price

  14. Generally I do not read post on blogs, but I
    would like to say that this write-up very compelled me to check out
    and do it! Your writing style has been surprised me. Thank you, quite
    nice article.

  15. How To Make A Profitable Window Repair Near Even If You’re
    Not Business-Savvy window repairs

  16. 5 Machines Coffee-Related Lessons From The Pros smart Home coffee Machines

  17. Are You Responsible For An Birth Defect Lawsuit Budget?
    10 Incredible Ways To Spend Your Money Birth Defect Lawsuits

  18. Vimeo.Com表示:

    The Most Underrated Companies To Monitor In The Online Shopping Sites For
    Clothes Industry Vimeo.Com

  19. manado toto表示:

    Fantastic goods from you, man. I have have in mind your
    stuff prior to and you are just extremely magnificent.
    I really like what you’ve acquired right here, really like what you are stating and the way in which during which
    you say it. You are making it entertaining and you continue to take care of to stay it smart.
    I cant wait to learn far more from you. That is really a
    great website.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。